import store from '@/store'
import _ from 'lodash'
export const imageError = {
  inserted(dom, option) {
    dom.onerror = () => {
      dom.src = option.value
    }
  },
  componentUpdated(dom, option) {
    dom.src = dom.src || option.value
  }
}

export const iif = {
  inserted(dom, option) {
    if (!option.value) {
      dom.parentNode.removeChild(dom)
    }
  }
}
export const sshow = {
  inserted(dom, option) {
    if (!option.value) {
      dom.style = 'display:none'
    }
  }
}

// 按钮权限指令
const checkPermission = (dom, option) => {
  const points = store.getters.userInfo.roles.points
  if (Array.isArray(option.value)) {
    if (!_.intersection(option.value, points).length) { // 判断俩个数组间有无相同项
      dom.remove()
    }
  } else {
    if (!points.includes(option.value)) {
      dom.remove()
    }
  }
}
// 权限指令
export const permission = {
  inserted: checkPermission,
  componentUpdated: checkPermission
}
